package view
{
	
	import events.*;
	
	import flash.events.MouseEvent;
	
	import models.Model;
	
	import mx.binding.utils.BindingUtils;
	import mx.binding.utils.ChangeWatcher;
	import mx.collections.ArrayCollection;
	import mx.controls.listClasses.ListBase;
	
	import spark.components.Button;
	import spark.components.DataGroup;
	import spark.components.List;
	import spark.components.supportClasses.SkinnableComponent;
	
	[Event(name="moveFocus", type="events.MoveFocusEvent")]
	[Event(name="changeStates", type="events.ChangeStatesEvent")]
	[Event(name="deleteSlide", type="events.DeleteSlideEvent" )]
	public class SlideShowPreview extends SkinnableComponent
	{
		[SkinPart] public var moveFocusLeft:Button;
		[SkinPart] public var moveFocusRight:Button;
		[SkinPart] public var startSlideshow:Button;
		[SkinPart] public var deleteSlide:Button;
		[SkinPart] public var imagesList:List;
		
		public var watcher:ChangeWatcher;
		
		[Bindable]
		public var model:Model;
		
		public function SlideShowPreview()
		{
			super();
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if (instance == moveFocusLeft)
			{
				moveFocusLeft.addEventListener(MouseEvent.CLICK, handleMoveFocusLeftClick);				
			}
			if (instance == moveFocusRight)
			{
				moveFocusRight.addEventListener(MouseEvent.CLICK, handleMoveFocusRightClick);		
			}
			if (instance==startSlideshow)
			{
				startSlideshow.addEventListener(MouseEvent.CLICK, handleChangeStatesClick);
			}
			if(instance == imagesList)
			{
				watcher = BindingUtils.bindProperty(imagesList,"dataProvider",model,"previewSlides");
			}
			if(instance == deleteSlide)
			{
				deleteSlide.addEventListener(MouseEvent.CLICK,handleDeleteSlide);
			}
		}
		
		private function handleMoveFocusLeftClick(event:MouseEvent):void
		{
			dispatchEvent(new MoveFocusEvent(MoveFocusEvent.MOVE,"left"));
		}
		private function handleMoveFocusRightClick(event:MouseEvent):void
		{
			dispatchEvent(new MoveFocusEvent(MoveFocusEvent.MOVE,"right" ));
		}
		private function handleChangeStatesClick(event:MouseEvent):void
		{
			dispatchEvent(new ChangeStatesEvent(ChangeStatesEvent.CHANGE,"ViewSlideShow"));
		}
		private function handleDeleteSlide(event:MouseEvent):void
		{
			dispatchEvent(new DeleteSlideEvent(DeleteSlideEvent.DELETE));
		}
	}
}